﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System.Windows.Controls;
using DevExpress.AgDataGrid.Data;
using System;
namespace DevExpress.AgDataGrid {
	public interface IAgDataGridSummaryOwner {
		void OnSummaryItemChanged();
	}
	public class AgDataGridSummaryItem : AgDataGridOwnedObject {
		string displayFormat;
		string fieldName;
		string showInColumn;
		string title;
		SummaryItemType summaryType;
		public AgDataGridSummaryItem() { }
		public AgDataGridSummaryItem(SummaryItemType summaryType, string fieldName)
			: this(summaryType, fieldName, string.Empty) {
		}
		public AgDataGridSummaryItem(SummaryItemType summaryType, string fieldName, string showInColumn) {
			this.fieldName = fieldName;
			this.summaryType = summaryType;
			this.showInColumn = showInColumn;
		}
		public string Title { 
			get { return title; }
			set { 
				if(title == value) return;
				title = value;
				OnSummaryItemChanged();
			}
		}
		public SummaryItemType SummaryType {
			get { return summaryType; }
			set {
				if(summaryType == value) return;
				summaryType = value;
				OnSummaryItemChanged();
			}
		}
		public string FieldName {
			get { return fieldName; }
			set {
				if(fieldName == value) return;
				fieldName = value;
				OnSummaryItemChanged();
			}
		}
		public string ShowInColumn {
			get { return showInColumn; }
			set {
				if(showInColumn == value) return;
				showInColumn = value;
				OnSummaryItemChanged();
			}
		}
		protected virtual void OnSummaryItemChanged() {
			if(Owner == null) return;
			Owner.OnSummaryItemChanged();
		}
		protected internal IAgDataGridSummaryOwner Owner { get; set; }
		protected internal SummaryItem SummaryItem { get; set; }
		protected internal override void SetOwner(object value) {
			Owner = value as IAgDataGridSummaryOwner;
		}
		public bool IsShownInColumn(string columnName) {
			if(!string.IsNullOrEmpty(ShowInColumn)) return ShowInColumn == columnName;
			return FieldName == columnName;
		}
		public bool IsInColumn(string columnName) {
			return FieldName == columnName;
		}
		public string DisplayFormat {
			get { return this.displayFormat; }
			set {
				if(value == this.displayFormat) return;
				displayFormat = value;
				OnSummaryItemChanged();
			}
		}
		public object GetSummaryValue() {
			object res = SummaryItem != null ? SummaryItem.SummaryValue : null;
			if(!string.IsNullOrEmpty(DisplayFormat)) {
				res = Convert.ToDouble(res).ToString(DisplayFormat);
			}
			return res;
		}
	}
}
